package com.moson.girl.controller;

import com.moson.girl.Repository.GirlRepository;
import com.moson.girl.domain.Girl;
import com.moson.girl.domain.Result;
import com.moson.girl.service.GirlService;
import com.moson.girl.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: MoXingjian
 * @Description:
 * @Date: Created in 17:24 2018/1/7
 * @Modified By:
 */
@RestController
public class GirlController {

    private final static Logger logger = LoggerFactory.getLogger(GirlController.class);

    @Autowired
    private GirlRepository girlRepository;
    @Autowired
    private GirlService girlService;

    /**
     * 查询所有女生
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList(){
        logger.info("query all girls");
        return girlRepository.findAll();
    }

    /**
     * 添加一个女生
     * @param girl
     * @return
     */
    @PostMapping(value = "/girl")
    public Result girlAdd(@Valid Girl girl, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            //验证不通过，输出错误信息并返回
            return ResultUtil.error(0,bindingResult.getFieldError().getDefaultMessage());
//            return null;  //如果是null，Aspect的After调用toString（）会抛异常
        }
        return ResultUtil.success(girlRepository.save(girl));
    }

    /**
     * 根据id查询一个女生
     * @param id 主键ID
     * @return
     */
    @GetMapping(value = "/girls/{id}")
    public Girl girlFindOne(@PathVariable("id") Integer id){
        return girlRepository.findOne(id);
    }

    /**
     * 更新
     * @param id
     * @param cupSize
     * @param age
     * @return
     */
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdateOne(@PathVariable("id") Integer id,
                              @RequestParam("cupSize") String cupSize,
                              @RequestParam("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return girlRepository.save(girl);
    }

    /**
     * 根据id删除一个女生
     * @param id
     */
    @DeleteMapping(value = "/girls/{id}")
    public void girlDeleteOne(@PathVariable("id") Integer id){
        girlRepository.delete(id);
    }

    /**
     * 根据年龄查询
     * @param age 主键ID
     * @return
     */
    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlFindByAgeAfter(@PathVariable("age") Integer age){
        return girlRepository.findByAgeAfter(age);
    }

    /**
     * 添加两个女生
     * @return
     */
    @PostMapping(value = "/girls/two")
    public void girlAddTwo(){
        girlService.insertTwo();
    }

    @GetMapping(value = "/girl/age/{id}")
    public void getAge(@PathVariable("id") Integer id) throws Exception{
        girlService.getAge(id);
    }
}
